/*
 * @Author: xiaosihan
 * @Date: 2021-04-30 21:34:51
 * @Last Modified by: xiaosihan
 * @Last Modified time: 2021-07-17 23:30:03
 */

import _ from "lodash";
import * as THREE from "three";
import Line, { LineProps } from "../Line/Line";



export interface LineSegmentsProps extends LineProps {

}

interface State { }

// 线段
export default class LineSegments extends Line<LineSegmentsProps> {
    constructor(props: LineSegmentsProps) {
        super(props);

        let name = this.getConstructorName();

        this.object3d = new (Function("THREELineSegments", `
            return class ${name} extends THREELineSegments {
            };
        `)(THREE.LineSegments));
    }

    // 设置线模型
    setLinGeometry() {

        const { bezier, duration = 500, points = [] } = this.props;

        // 保证点数为双数
        if (points.length % 2 === 1) {
            points.length = points.length - 1;
        }

        const { lineGeometry } = this;

        let newPoints = _.flatten(points);// 把传入的参数转成顶点格式 一维数组

        this.TLineVector3.setBezier(bezier).setDuration(duration).set(newPoints);

        // 长度不相等时 重新创建 线条position顶点对象
        if (lineGeometry.attributes.position.array.length < newPoints.length) {

            let originPoints = lineGeometry.attributes.position.array;

            let newPosition = new THREE.Float32BufferAttribute(newPoints, 3)

            newPosition.set(originPoints);

            lineGeometry.setAttribute("position", newPosition);
        }

        lineGeometry.drawRange.count = points.length;
    }

}